package com.wugh.fast.security.social.config;

import com.wugh.fast.kernel.core.props.FastProperties;
import com.wugh.fast.kernel.core.props.SecurityProperties;
import com.wugh.fast.kernel.core.props.SocialSecurityProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.security.SocialAuthenticationFilter;
import org.springframework.social.security.SpringSocialConfigurer;

/**
 * 说明:
 *
 * @author wugh
 * @date 2018-09-25 11:13
 */
public class FastSocialSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    protected FastProperties fastProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.apply(new SpringSocialConfigurer() {
            @Override
            protected <T> T postProcess(T object) {
                SocialAuthenticationFilter filter = (SocialAuthenticationFilter) super.postProcess(object);
                SecurityProperties security = fastProperties.getSecurity();
                SocialSecurityProperties social = security.getSocial();
                if (StringUtils.isNotBlank(social.getFilterProcessUrl())) {
                    filter.setFilterProcessesUrl(social.getFilterProcessUrl());
                }
                if (StringUtils.isNotBlank(security.getSignUpUrl())) {
                    //配置设计登录失败自动跳转的注册页面
                    filter.setSignupUrl(security.getSignUpUrl());
                }
                if (StringUtils.isNotBlank(security.getSignInUrl())) {
                    //登录失败,自动跳转登录页面
                    filter.setDefaultFailureUrl(security.getSignInUrl());
                }
                return (T) filter;
            }
        });
    }
}


